package com.qf.lanzhou.blog.api.controller;

import com.qf.lanzhou.blog.api.pojo.entity.User;
import com.qf.lanzhou.blog.api.service.UserService;
import com.qf.lanzhou.blog.api.service.impl.UserServiceImp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Yeach-chen
 * @ClassName ModifyPersonalDataServlet
 */
@WebServlet("/personalMessage.do")
public class ModifyPersonalDataServlet extends BaseServletPost {
    private UserService userService=new UserServiceImp();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取新邮箱
        String newEmail = req.getParameter("newEmail");
        System.out.println("新邮箱："+newEmail);
        String newPhone = req.getParameter("newPhone");
        System.out.println("新电话："+newPhone);
        User user = (User)req.getSession().getAttribute("user");
        System.out.println(user.toString()+"-----------");
        int result = 0;
        try {
            result=userService.modifyUserMsg(newEmail, newPhone, user);
            User dbUser = userService.getByUserName(user.getUserName());
            req.getSession().setAttribute("user",dbUser);
            successResultWrite(result,resp);
        }catch (RuntimeException e){
            String message = e.getMessage();
            failedResultWrite(message,resp);
        }
    }
}
